﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace kindly
{
    public partial class Check : LoggingPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string result;
            List<FileUpdates> updates = null;

            try
            {
                string attachmentPath = Server.MapPath("~/" + kindly.Properties.Settings.Default.attch_path);

                // check emails
                Mail.Get(attachmentPath, false);

                // check current files
                updates = Files.GetFileUpdates(attachmentPath);

                result = "OK";
            }
            catch (Exception ex)
            {
                result = string.Format("Error occured while checking for new items: {0}", ex.Message);
            }

            // write JSON result
            JSONCheckResult jsonresult = new JSONCheckResult
            {
                Result = result,
                FileUpdates = updates
            };
            jsonresult.WriteResponse();
        }

        public class JSONCheckResult : JSONSimpleResult
        {
            private List<FileUpdates> fileUpdates;

            public List<FileUpdates> FileUpdates
            {
                get { return fileUpdates; }
                set { fileUpdates = value; }
            }
        }
    } 
}